﻿using System;
using System.Collections.Generic;
using System.Text;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.IBatisRepository;
using OneMG.Phoenix.Respository;
using System.Data;
using OneMG.Phoenix.Entity;

namespace OneMG.Phoenix.Service
{
    public class LinksService : ILinksService
    {
		 ILinks links = new LinksRepository();
		 
        public System.Int32 AddLinks(LinksModel model)
        {
			ELinks entity = ConvertModelToEntity(model);
			
            return links.AddLinks(entity);
        }

		public void UpdateByLinkID (LinksModel model)
		{
			ELinks entity = ConvertModelToEntity(model);

            links.UpdateByLinkID(entity);

		}

		public void DeleteByLinkID (System.Int32 linkID)
		{
		
            links.DeleteByLinkID(linkID);

		}
		 
        public LinksModel GetByLinkID (System.Int32 linkID)
        {
        
			ELinks entity = links.GetByLinkID(linkID);
			
            LinksModel model = ConvertEntityToModel(entity);


            return model;
        }

        public IList<LinksModel> GetPagedList(int pageSize, int pageIndex, out int totalRecord)
        {
            IList<ELinks> entityList = links.GetPagedList(pageSize, pageIndex, out totalRecord);

            if (entityList == null)
                return null;

            IList<LinksModel> modelList = new List<LinksModel>();

            foreach (var item in entityList)
            {
                LinksModel model = ConvertEntityToModel(item);
                if (model != null)
                    modelList.Add(model);
            }
            return modelList;


        }

        
		public LinksModel ConvertEntityToModel(ELinks entity)
		{
			if (entity == null)
            {
                return null;
            }
			LinksModel model = new LinksModel();
                        model.LinkID = entity.LinkID;
                        model.LinkUrl = entity.LinkUrl;
                        model.LinkName = entity.LinkName;
                        model.LinkSort = entity.LinkSort;
                        model.CreateTime = entity.CreateTime;
                        model.UpdateTime = entity.UpdateTime;
                        model.CreateUser = entity.CreateUser;
                        model.UpdateUser = entity.UpdateUser;
                        return model;
		}
		
		public ELinks ConvertModelToEntity(LinksModel model)
		{
			if (model == null)
            {
                return null;
            }
			ELinks entity = new ELinks();
                        entity.LinkID = model.LinkID;
                        entity.LinkUrl = model.LinkUrl;
                        entity.LinkName = model.LinkName;
                        entity.LinkSort = model.LinkSort;
                        entity.CreateTime = model.CreateTime;
                        entity.UpdateTime = model.UpdateTime;
                        entity.CreateUser = model.CreateUser;
                        entity.UpdateUser = model.UpdateUser;
                        return entity;
		}
    }
}